// @Author EthanScriptOn
// @Desc
package log_task

import (
	"gitee.com/fatzeng/srf_switch_log/log"
	"gitee.com/fatzeng/srf_switch_log/log_define"
	"gitee.com/fatzeng/srf_switch_log/log_format"
	"strings"
)

type LogUnitTask struct {
	unit      *log.LogSnapshot
	rule      *log.RuleBinding
	formatStr string
}

func GenerateLogUnitTask(rule *log.RuleBinding, unit *log.LogSnapshot) *LogUnitTask {
	return &LogUnitTask{
		unit: unit,
		rule: rule,
	}
}

func (l *LogUnitTask) GetUnit() *log.LogSnapshot {
	return l.unit
}

func (l *LogUnitTask) GetRule() *log.RuleBinding {
	return l.rule
}

func (l *LogUnitTask) Execute() error {
	l.formatStr = l.formatOutputContent(l.rule.OutPutTemplate, l.unit)
	return nil
}

func (l *LogUnitTask) GetFormatStr() string {
	return l.formatStr
}

func (l *LogUnitTask) formatOutputContent(bits []log_define.LogSnapshotOutBit, parser log_format.LogTemplateParser) string {
	builder := strings.Builder{}
	builder.WriteString(parser.TemplateParser(bits))
	builder.WriteString("\r\n")
	return builder.String()
}
